package com.itts.middle;

import cn.hutool.core.util.IdUtil;
import com.itts.middle.config.cros.CrosFilter;
import com.itts.middle.quartz.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;

@EnableAsync(proxyTargetClass = true)
@Slf4j
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = "com.itts")
@MapperScan({"com.itts.*.mapper*", "com.itts.*.*.mapper", "com.itts.*.*.*.mapper"})
public class middeleApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(middeleApplication.class, args);

        WebApplicationContext webApplicationContext = (WebApplicationContext) context;
        ServletContext servletContext = webApplicationContext.getServletContext();
        String UUID = IdUtil.fastSimpleUUID();
        servletContext.setAttribute("SERVER_UUID", UUID);
        log.info("Application UUID:" + UUID);

        String serverPort = context.getEnvironment().getProperty("server.port");
        log.info("Application started at http://localhost:" + serverPort);
    }

    @Bean
    public SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }

    /**
     * 过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean registerFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.addUrlPatterns("/*"); // 过滤掉所有的请求
        bean.setFilter(new CrosFilter());
        return bean;
    }
}
